package cn.lingyangwl.agile.infra.job.controller;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.lingyangwl.agile.file.excel.CusExcelExportUtil;
import cn.lingyangwl.agile.infra.job.model.entity.JobLog;
import cn.lingyangwl.agile.infra.job.model.rqrs.JobLogPageReq;
import cn.lingyangwl.agile.infra.job.service.IJobLogService;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.framework.core.response.Resp;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 调度日志操作处理
 * 
 * @author shenguangyang
 */
@RestController
@RequestMapping("/job/log")
public class JobLogController {
    @Resource
    private IJobLogService jobLogService;

    /**
     * 查询定时任务调度日志列表
     */
    @RamResource(action = "monitor:jobLog:query", name = "查询定时任务调度日志", type = ActionType.READ)
    @GetMapping("/page")
    public Resp<IPage<JobLog>> page(JobLogPageReq req) {
        IPage<JobLog> page = jobLogService.pageJobLog(req);
        return Resp.ok(page);
    }

    /**
     * 导出定时任务调度日志列表
     */
    @RamResource(action = "monitor:jobLog:export", name = "导出定时任务调度日志列表", type = ActionType.READ)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JobLogPageReq req) throws Exception {
        List<JobLog> list = jobLogService.listJobLog(req);
        ExportParams params = new ExportParams();
        params.setType(ExcelType.HSSF);
        CusExcelExportUtil.exportExcel(response, params, "调度日志.xls", JobLog.class, list);
    }

    /**
     * 根据调度编号获取详细信息
     */
    @RamResource(action = "monitor:jobLog:get", name = "查询定时任务日志详情", type = ActionType.READ)
    @GetMapping(value = "/getById")
    public Resp<JobLog> getById(@RequestParam("id") Long jobLogId) {
        return Resp.ok(jobLogService.getById(jobLogId));
    }

    /**
     * 删除定时任务调度日志
     */
    @RamResource(action = "monitor:jobLog:delete", name = "删除定时任务调度日志", type = ActionType.WRITE)
    @DeleteMapping("/delete")
    public Resp<?> delete(@RequestParam(value = "id") Long jobLogId) {
        return Resp.ok(jobLogService.deleteById(jobLogId));
    }

    /**
     * 批量删除定时任务调度日志
     * @param jobLogIds 任务日志ids
     */
    @RamResource(action = "monitor:jobLog:delete", name = "删除定时任务调度日志", type = ActionType.WRITE)
    @DeleteMapping("/deleteBatch")
    public Resp<?> deleteBatch(@RequestParam(value = "ids") Long[] jobLogIds) {
        return Resp.ok(jobLogService.deleteByIds(jobLogIds), "删除失败");
    }


    /**
     * 清空定时任务调度日志
     */
    @RamResource(action = "monitor:jobLog:clean", name = "清空定时任务调度日志", type = ActionType.WRITE)
    @DeleteMapping("/clean")
    public Resp<?> clean() {
        jobLogService.cleanJobLog();
        return Resp.ok();
    }
}